package com.apexedu.biz.service.impl;

import com.apexedu.biz.action.ResetPasswordAction;
import com.apexedu.framework.service.impl.AbstractBaseServiceSupport;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.Pager;
import com.apexedu.framework.util.SysPropertiesUtil;
import com.apexedu.identity.IdConstants;
import com.apexedu.identity.action.IdLoginAction;
import com.apexedu.identity.entity.TSysUser;
import com.apexedu.sys.SysConstant;
import com.apexedu.sys.util.BusinessLogUtil;
import com.apexedu.util.StringUtil;
import com.apexedu.util.crypto.LiveBosMD5;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by hc on 2014/12/24.
 */
@Service("resetPasswordService")
public class ResetPasswordServiceImpl extends AbstractBaseServiceSupport {

    private static final Log LOG = LogFactory.getLog(ResetPasswordServiceImpl.class);

    @Override
    public List<?> list(Object myaction, Pager pager) throws Exception {
        ResetPasswordAction action = (ResetPasswordAction) myaction;
        String sqlColumn = "select a.userid,a.userloginid,a.username,a.state,c.name as orname,b.sfzh as sfzh";
        String sql = " from t_sys_user a inner join tuser b on a.userloginid=b.userid" +
                " left join lborganization c on c.id=b.orgid" +
                " where a.userloginid not like '%admin%'";
        TSysUser tsysUser = action.getTsysUser();
        List<Object> params = new LinkedList<Object>();
        if (tsysUser != null) {
            if (StringUtil.isNotBlank(tsysUser.getState()) && !"-1".equals(tsysUser.getState())) {
                sql += " and a.state= ?";
                params.add(tsysUser.getState());
            }
            if (StringUtil.isNotBlank(tsysUser.getUsername())) {
                sql += " and a.username like ?";
                params.add("%" +tsysUser.getUsername()+ "%");
            }
            if (StringUtil.isNotBlank(tsysUser.getUserloginid())) {
                sql += " and a.userloginid like ?";
                params.add("%"+tsysUser.getUserloginid()+ "%");
            }
            if (StringUtil.isNotBlank(tsysUser.getSfzh())) {
                sql += " and b.sfzh like ?";
                params.add("%"+tsysUser.getSfzh()+ "%");
            }
        }
        pager.setTotalRows(DBUtil.count(sql,params.toArray()));
        List<TSysUser> tsysUsers = DBUtil.queryPageBeanList(pager, sqlColumn + sql + " order by b.yhlx,c.id,userloginid", TSysUser.class,params.toArray());
        return tsysUsers;
    }

    @Override
    public boolean remove(Object o) throws Exception {
        return false;
    }

    @Override
    public void saveOrUpdate(Object o) throws Exception {
    }

    @Override
    public void load(Object o) throws Exception {
    }

    /**
     * 重置密码
     *
     * @param action
     * @return
     */
    public boolean doReset(ResetPasswordAction action) {
        String userid = action.getWid();
        String sfzh = (String) DBUtil.queryFieldValue("select sfzh from tuser where userid = (select userloginid from t_sys_user where userid=?)", userid);
        String last;
        if (StringUtil.isNotBlank(sfzh)) {
            last = sfzh.substring(sfzh.length() - 6);
        } else {
            last = SysPropertiesUtil.get(IdConstants.INITIAL_PASSWORD);
        }
        String userloginid = (String) DBUtil.queryFieldValue("select userloginid from t_sys_user where userid = ?",userid);
        int count = DBUtil.executeSQL("update t_sys_user set userpwd = ? where userid = ?", LiveBosMD5.getMD5Digest(last).toUpperCase(), userid);
        if (count > 0) {
            BusinessLogUtil.log(action.getLoginuser().getUserloginid(), SysConstant.CZDX_T_SYS_USER,
                    SysConstant.LOG_ACTION_UPDATE, IdLoginAction.getIpAddr(action.getRequest()), userloginid+"的密码重置成功");
            return true;
        } else {
            return false;
        }
    }
}
